package com.ysd.lis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkQualityDetail;
import com.ysd.lis.mapper.WkQualityDetailMapper;
import com.ysd.lis.request.QualityDetailListParams;
import com.ysd.lis.service.WkQualityDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 质量控制计划明细表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-05-08
 */
@Service
public class WkQualityDetailServiceImpl extends ServiceImpl<WkQualityDetailMapper, WkQualityDetail> implements WkQualityDetailService {

    @Autowired
    WkQualityDetailMapper wkQualityDetailMapper;

    @Override
    public Result getQualityDetailList(QualityDetailListParams QualityDetailListParams) {
        LambdaQueryWrapper<WkQualityDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkQualityDetail::getDelFlag, 0)
                .eq(WkQualityDetail::getPlanId, QualityDetailListParams.getPlanId());
        List<WkQualityDetail> wkQualityDetails = wkQualityDetailMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", wkQualityDetails);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result addQualityDetail(List<WkQualityDetail> wkQualityDetails) {
        LambdaQueryWrapper<WkQualityDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkQualityDetail::getPlanId, wkQualityDetails.get(0).getPlanId());
        int delete = wkQualityDetailMapper.delete(lambdaQueryWrapper);
        boolean save = this.saveBatch(wkQualityDetails);
        if (save) {
            return Result.succ(1, "插入成功！", delete);
        } else {
            return Result.fail("插入失败！");
        }
    }

    @Override
    public Result addQualityDetail(WkQualityDetail wkQualityDetail) {
        boolean save = this.save(wkQualityDetail);
        if (save) {
            return Result.succ(1, "新增成功！", save);
        } else {
            return Result.fail("新增失败！");
        }
    }
    @Override
    public Result editQualityDetail(WkQualityDetail wkQualityDetail) {
        boolean save = this.updateById(wkQualityDetail);
        if (save) {
            return Result.succ(1, "编辑成功！", save);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result deleteQualityDetail(List<String> ids) {
        boolean save = this.removeByIds(ids);
        if (save) {
            return Result.succ(1, "删除成功！", save);
        } else {
            return Result.fail("删除失败！");
        }
    }
}
